<?php
namespace app\service;

class LyricsService
{


    public function formatDescription($content)
    {
        $content = json_decode($content,true);
        $description = '';
        foreach($content as $line){
            $description .= $line['pinyin'] . ' ';
        }
        $description = mb_substr(str_replace(["\n", "\r", "\t"], '', strip_tags($description)), 0, 150) . '...';
        return $description;
    }
    public function getHreflangsByLyric($lyric)
    {
        $hreflangs = '';
        $language_relation = [];
        $available_langs = array_keys(config('app.available_langs'));
        $language_relation[$lyric['language']] = $lyric['title'];
        
        $language_relation_titles = $lyric['language_relation_titles'];
        if (!empty($language_relation_titles)){
            $language_relation_titles = json_decode($language_relation_titles,true);
            foreach ($language_relation_titles as $key => $value) {
                $language_relation[$key] = $value;
            }
        }
        foreach ($available_langs as $lang) {
            $language_relation_langs = array_keys($language_relation);
            if (in_array($lang, $language_relation_langs)){
                $title = $language_relation[$lang];
                $uri = "lyrics/".rawurlencode($title);
                $url = get_full_url_by_uri($lang,$uri);
                $url = 'https://'. config('site_domain').$url;
                $hreflangs .= '<link rel="alternate" hreflang="'.$lang.'" href="'.$url.'" />'.PHP_EOL;
            }
        }
        return $hreflangs;
    }
}